package com.example.duanjunjie.learn.wk;

import android.os.Handler;
import android.os.Message;

/**
 * Created by fengjian on 2018/5/21.
 */

// 悟空Notice类型消息管理类
public class WKNoticeManager2 {
    private static WKNoticeManager2 instance;

    private WKNoticeMessageHandlerThread mWKNoticeMessageHandlerThread;
    private Handler mWorkHandler;

    private WKMessageListenerDefault listenerDefault;
    private WKListenerList<WKMessageListener> listenerList;

    private WKNoticeManager2() {
        init();
    }

    public static WKNoticeManager2 getInstance() {
        if (instance == null) {
            synchronized (WKNoticeManager2.class) {
                if (instance == null) {
                    instance = new WKNoticeManager2();
                }
            }
        }
        return instance;
    }

    private void init() {
        try {
            listenerList = new WKListenerList<WKMessageListener>();
            listenerDefault = new WKMessageListenerDefault();
            listenerList.add(listenerDefault);

            mWKNoticeMessageHandlerThread = new WKNoticeMessageHandlerThread("WKNoticeMessageHandlerThread", listenerList);
            mWKNoticeMessageHandlerThread.start();
            mWorkHandler = mWKNoticeMessageHandlerThread.getWorkHandler();
        } catch (Exception e) {
        }
    }

    public void registerListener(WKMessageListener listener) {
        if (listener != null && mWorkHandler != null) {
            Message message = mWorkHandler.obtainMessage();
            message.what = WKNoticeMessageHandlerThread.MSG_ADD_LISTENER;
            message.obj = listener;
            mWorkHandler.sendMessage(message);
        }
    }

    public void unregisterListener(WKMessageListener listener) {
        if (listener != null && mWorkHandler != null) {
            Message message = mWorkHandler.obtainMessage();
            message.what = WKNoticeMessageHandlerThread.MSG_REMOVE_LISTENER;
            message.obj = listener;
            mWorkHandler.sendMessage(message);
        }
    }

    public void handleWKNoticeMessage(WKNoticeMessagePacket packet) {
        if (packet != null && mWorkHandler != null) {
            Message message = mWorkHandler.obtainMessage();
            message.what = WKNoticeMessageHandlerThread.MSG_HANDLE_WKMESSAGE;
            message.obj = packet;
            mWorkHandler.sendMessage(message);
        }
    }

    public void onWKConnectionEstablished() {
        if (mWorkHandler != null) {
            Message message = mWorkHandler.obtainMessage();
            message.what = WKNoticeMessageHandlerThread.MSG_CONNECT_ESTABLISHED;
            mWorkHandler.sendMessage(message);
        }
    }

    public void onWKConnectionLost() {
        if (mWorkHandler != null) {
            Message message = mWorkHandler.obtainMessage();
            message.what = WKNoticeMessageHandlerThread.MSG_CONNECT_LOST;
            mWorkHandler.sendMessage(message);
        }
    }

    public void release() {
        if (listenerList != null && listenerDefault != null) {
            listenerList.remove(listenerDefault);
        }
        if (mWKNoticeMessageHandlerThread != null) {
            mWKNoticeMessageHandlerThread.quit();
        }
    }

}
